<template>
  <div>
    <el-button size="mini"
               type="success"
               style="margin-bottom: 10px"
               icon="el-icon-plus"
               :disabled="maturity ==='已签' || isDisabled"
               @click="addMonthPlan">新增计划</el-button>
    <!-- 表格  -->
    <el-table :data="tableData"
              border
              stripe>
      <el-table-column prop="planMonth"
                       label="计划月份"
                       align="center">
      </el-table-column>
      <el-table-column prop="projectMaturity"
                       label="成熟度"
                       align="center">
      </el-table-column>
      <el-table-column prop="upPersonnel"
                       label="更新人员"
                       align="center"></el-table-column>
      <el-table-column prop="time"
                       label="更新日期"
                       align="center"></el-table-column>
      <el-table-column label="操作"
                       width="50"
                       align="center">
        <template slot-scope="scope">
          <el-button type="text"
                     size="mini"
                     width="50"
                     :disabled="voidEdit(scope.row)"
                     @click="editMonthPlan(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :total="pagging.total"
                   :page-sizes="pagging.sizes"
                   :current-page="pagging.currentPage"
                   @size-change="sizeChange"
                   @current-change="pageChange">
    </el-pagination>
    <trace-detail-month-plan :contract-id="contractId"
                             :maturity="maturity"
                             @close="getTableData"></trace-detail-month-plan>
  </div>
</template>

<script>
import { getPlanProcess } from '@/api/maintenance'
import TraceDetailMonthPlan from './trace-detail-month-plan'
import Bus from '@/utils/bus'
import Moment from 'moment'
export default {
  props: {
    contractId: {
      type: String
    },
    mode: {
      type: Number,
      default: 1
    },
    maturity: {
      type: String
    }
  },
  data() {
    return {
      tableData: [],
      isDisabled: false,
      // 分页属性
      pagging: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        sizes: [10, 30, 50, 100]
      }
    }
  },
  methods: {
    search() {
      this.pagging.currentPage = 1
      this.getTableData()
    },
    // 获取数据
    getTableData() {
      const params = {
        contractId: this.contractId,
        pageSize: this.pagging.pageSize,
        pageIndex: this.pagging.currentPage
      }
      getPlanProcess(params).then(res => {
        if (res.data.success) {
          const result = res.data.data
          this.tableData = result.data
          this.pagging.total = result.totalCount
          this.pagging.currentPage = result.pageIndex
          this.pagging.pageSize = result.pageSize

          // 取项目计划记录(倒序)，判断第一条记录,是否可编辑，如果可编辑限制新增。
          if (Moment(this.tableData[0].planMonth).format('YYYY-MM-DD') > Moment().format('YYYY-MM-02')) {
            this.isDisabled = true
          } else {
            this.isDisabled = false
          }
        }
      })
    },
    // 新增项目月份计划
    addMonthPlan() {
      Bus.$emit('open-month-plan', 1, null)
    },
    // 编辑项目月份计划
    editMonthPlan(row) {
      Bus.$emit('open-month-plan', 2, row)
    },
    // 验证是否可修改
    voidEdit(row) {
      // 如何计划时间为历史
      if (Moment(row.planMonth).format('YYYY-MM-DD') < Moment().format('YYYY-MM-02')) {
        return true
      } else {
        return false
      }
    },
    // 每页显示条数变更新
    sizeChange(size) {
      this.pagging.pageSize = size
      this.getTableData()
    },
    // 跳转到页面
    pageChange(curPage) {
      this.pagging.currentPage = curPage
      this.getTableData()
    }
  },
  components: {
    TraceDetailMonthPlan
  }
}
</script>
